package user;

import java.sql.ResultSet;
import java.util.ArrayList;

import org.apache.log4j.Logger;

import sun.security.jca.GetInstance;
import utils.Database;

public class UserBeanDAO {

	private static Logger jlog = Logger.getLogger(UserBeanDAO.class);
	private Database db;
	
	private ArrayList<String[]> usrLst;
	
	public UserBeanDAO()
	{
		
		db = Database.getInstance();
	}
	
	public ArrayList<Object> getUsr(String username)
	{
		ArrayList<Object> User = new ArrayList<Object>();
		db.openConn();
		try
		{
			ResultSet rs = db.connect_and_execute("SELECT fullname, location, admin, inactive FROM users WHERE username='" + username+ "'");
			if(rs.next())
			{
				User.add(rs.getString(1));
				User.add(rs.getString(2));
				User.add(rs.getBoolean(3));
				User.add(rs.getBoolean(4));
			}
			
		}
		catch (Exception e)
		{
			jlog.info("Exception UserBeanDAO.getUsr:" + e.getMessage());
		}
		db.closeConn();
		return User;
	}
	
	public String[] login(String username, String password)
	{
		String[] result = new String[5];
		db.openConn();
		try
		{
		
			ResultSet rs = db.connect_and_execute("select fullname, location, admin, inactive from users where username='" + username +"' and password='" + password + "'");
			if(rs.next())
			{
				result[0] = rs.getString(1);
				result[1] = rs.getString(2);
				result[2] = Boolean.toString(rs.getBoolean(3));
				result[3] = Boolean.toString(rs.getBoolean(4));
				result[4] = Boolean.toString(true);
				rs.close();
				db.closeConn();
				return result;
			}
			else
			{
				jlog.info("login failed for User:" + username);
				result[0] = Boolean.toString(false);
				result[1] = Boolean.toString(false);
				result[2] = Boolean.toString(false);
				result[3] = Boolean.toString(false);
				result[4] = Boolean.toString(false);
				rs.close();
				db.closeConn();
				return result;
			}
		}
		catch(Exception e)
		{
			jlog.info(e.getMessage());
		}
		db.closeConn();
		return result;
		
		
	}
	
	public boolean deleteUser(String username)
	{
		db.openConn();
		try
		{
			db.connect_and_execute("DELETE FROM users WHERE username='" + username +"'");
		}
		catch(Exception sqlex)
		{
			jlog.info("Exception sqlex:" + sqlex.getMessage());
			db.closeConn();
			return false;
		}
		db.closeConn();
		return true;
	}
	
	public boolean addUser(String username, String hash, String fullname, String location, boolean admin, boolean inactive)
	{
		db.openConn();
		try
		{			
			db.connect_and_execute("INSERT INTO users (username, password, fullname, location, admin, inactive)" + " "
										+ "VALUES ('" + username + "','" + hash + "','" + fullname + "','" + location + "','" + admin + "','" + inactive + "')");	
		}
		catch(Exception sqlex)
		{
			jlog.info("Exception sqlex:" + sqlex.getMessage());
			db.closeConn();
			return false;
		}
		db.closeConn();
		return true;
	}
	public boolean updateUser(String username, String typ, String value)
	{
		db.openConn();
		try
		{
			if (typ.equals("admin") || typ.equals("inactive"))
			{
				db.connect_and_execute("UPDATE users SET " + typ + "=" + value + " WHERE username='" + username + "'");
			}
			else
			{
				db.connect_and_execute("UPDATE users SET " + typ + "='" + value + "' WHERE username='" + username + "'");
			}
		}
		catch(Exception sqlex)
		{
			jlog.info("Exception sqlex:" + sqlex.getMessage());
			db.closeConn();
			return false;
		}
		db.closeConn();
		return true;
	}
	
	public ArrayList<String[]> getUsrLst()
	{
		db.openConn();
		try
		{
			ResultSet rs = db.connect_and_execute("SELECT username, fullname, location, admin, inactive FROM users");
			
			usrLst = new ArrayList<String[]>();
			
			//String[] usr = new String[5];
			
			while(rs.next())
			{
				String[] usr = new String[5];
				
				usr[0] = rs.getString(1);
				usr[1] = rs.getString(2);
				usr[2] = rs.getString(3);
				usr[3] = Boolean.toString(rs.getBoolean(4));
				usr[4] = Boolean.toString(rs.getBoolean(5));
				
				usrLst.add(usr);
				
				//usr = null;
			}
			rs.close();
		}
		catch (Exception e)
		{
			jlog.info("Exception @ UserBeanDAO.getUsrLst():" + e.getMessage());
			db.closeConn();
			return null;
		}
		db.closeConn();
		return usrLst;
	}
	
	
}
